/*
 * @Descripttion: your project
 * @version: 1.0
 * @Author: hongweixun
 * @Date: 2022-05-31 08:24:47
 * @LastEditors: hongweixun
 * @LastEditTime: 2022-07-01 09:42:04
 */
/* eslint-disable no-undef */
import axios from 'axios'
/**
 * http请求工具类
 *
 * 请求拦截器 负责将客户端标识token值存储并放置在头部提交给服务端
 *
 * 响应拦截器 负责全局处理业务请求的网络或者业务错误
 */
// 创建axios的实例
// 环境的切换
if (import.meta.env.MODE === 'development') {
    axios.defaults.baseURL = '/api/' // 开发环境
} else if (import.meta.env.MODE === 'debug') {
    axios.defaults.baseURL = '' // 调试环境
} else if (import.meta.env.MODE === 'production') {
    axios.defaults.baseURL = '' // 生产环境
}
axios.defaults.timeout = 10000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8;multipart/form-data'
// const Server = axios.create({
//     baseURL: process.env.NODE_ENV === 'production' ? '' : 'http://campus.hlzhjy.com:50080'
//     // 超时时间
//     // timeout:20000
// })
// 请求拦截
axios.interceptors.request.use((config?: any) => {
    if (window.sessionStorage.getItem('accessToken')) {
        config.headers.accessToken = window.sessionStorage.getItem('accessToken')
    } else {
        // 班主任
        config.headers.accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlX25hbWUiOiJcdTczZWRcdTRlM2JcdTRlZmIiLCJpZCI6IjEwMDAyMTg3Iiwicm9sZXR5cGUiOiIxMCIsIm5hbWUiOiJcdTZkMmFcdTRmMWZcdTUyY2ItVCIsImluY2giOiIvU2Nob29sVXNlci9QdWJsaWMvaW5jaC9EUGE2QkZRZjRRY3lkbWtRNDVXbXdXc3hoWmsyRkdtWS5qcGciLCJzY2hvb2xfbWFuYWdlcmlkIjoiMTAwMTkyMjAiLCJsb2dpbl90eXBlIjoxLCJ0aW1lIjoxNjU2NjM5ODA5fQ.5WFrQMK5DwWQcWCs7jrh82Y3wWePAyJ6rlzbjRSkLPk'
        // console.log(config)
        // 年纪长
        // config.headers.accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlX25hbWUiOiJcdTVlNzRcdTdlYTdcdTk1N2YiLCJpZCI6IjEwMDE5MTkxIiwicm9sZXR5cGUiOiIxMiIsInZpc2l0b3JzX3R5cGUiOjEyLCJuYW1lIjoiXHU1ZWQ2XHU1ZmQ3XHU3MTRjLVQiLCJpbmNoIjoiL1NjaG9vbFVzZXIvUHVibGljL2luY2gvemlBM3hQQVQyRTdRM0hodHJaVHRDd2FmR1ljeTVEU3QuanBnIiwiZ2VuZGVyIjoiMSIsInBob25lIjoiMTM4MjUzNDE5MDYiLCJyb2xlX3R5cGUiOjIsInNjaG9vbF9tYW5hZ2VyaWQiOiIxMDAxOTIyMCIsImlzX2FkbWluIjpmYWxzZSwibG9naW5fdHlwZSI6Mn0.KBrns3spTXT7NNzw_HpopAivmFrvuJ7vPEDt88vui68'
        // 家长
        // config.headers.accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlX25hbWUiOiJcdTViYjZcdTk1N2YiLCJpZCI6IjUwMDAwMDI2Iiwicm9sZXR5cGUiOiI4IiwidmlzaXRvcnNfdHlwZSI6MCwibmFtZSI6Ilx1NTIxOFx1Njc5Ny1TLVx1NWJiNlx1OTU3ZiIsImluY2giOiIvU2Nob29sVXNlci9QdWJsaWMvaW5jaC8xLmpwZyIsImdlbmRlciI6bnVsbCwicGhvbmUiOiIxODgyNTczMzQ0NSIsInJvbGVfdHlwZSI6MSwic2Nob29sX21hbmFnZXJpZCI6IjEwMDE5MjIwIiwiaXNfYWRtaW4iOmZhbHNlLCJsb2dpbl90eXBlIjoyfQ.n_xR2Ek8sw3GwKCnCYy1krOSf94nF9Ful1GgSbqBeQw'
    }
    return config;
}, (err: any) => {
    Promise.reject(err)
})
// 响应拦截
axios.interceptors.response.use((config?: any) => {
    return config
})
// export default Server;

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(url: any, params?: any) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err.data)
        })
    })
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(url: string, params: object) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err.data)
            })
    })
}
/**
 * put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function put(url: string, params: object) {
    return new Promise((resolve, reject) => {
        axios.put(url, params)
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err.data)
            })
    })
}